package logic

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"manage/managerpc/internal/svc"
	"manage/managerpc/managerpc"
	"manage/models/mysql"
	"manage/models/redis"
	"manage/pkg"
)

type FindLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFindLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FindLogic {
	return &FindLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 找回用户信息
func (l *FindLogic) Find(in *managerpc.FindRequest) (*managerpc.FindResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.MerageUser
	mysql.DB.Where("account", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("用户不存在")
	}
	get := redis.Rdb.Get(l.ctx, "sendsms"+in.Account)
	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("验证码不正确")
	}
	if user.Password == pkg.Md5(in.Password) {
		return nil, fmt.Errorf("不能与原密码一至")
	}
	user.Password = pkg.Md5(in.Password)
	mysql.DB.Updates(&user)
	return &managerpc.FindResponse{
		Id: int64(user.ID),
	}, nil
}
